﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Client.XML;

namespace Client.UserControls
{
    [ToolboxItem(true)]
    public partial class WorkPlanItem : UserControl
    {
        public WorkPlanItem()
        {
            InitializeComponent();
        }

        private WorkPlan parent = null;
        public WorkPlan ParentWP
        {
            get { return this.parent; }
            set { this.parent = value; }
        }

        public WorkPlanItem(WorkPlan pParent)
        {
            InitializeComponent();
            this.parent = pParent;
        }

        public string StartTime
        {
            get { return this.dtStart.Text; }
            set { this.dtStart.Text = value; }
        }

        public DateTime StartTimeDT
        {
            get { return this.dtStart.Value; }
            set { this.dtStart.Value = value; }
        }

        public string EndTime
        {
            get { return this.dtEnd.Text; }
            set { this.dtEnd.Text = value; }
        }

        public DateTime EndTimeDT
        {
            get { return this.dtEnd.Value; }
            set { this.dtEnd.Value = value; }
        }

        public bool IsAllowUseComputer
        {
            get { return this.chkUse.Checked; }
            set { this.chkUse.Checked = value; }
        }

        public string PlanContext
        {
            get { return this.txtContext.Text; }
            set { this.txtContext.Text = value; }
        }

        public PlanItem GetPlanItem()
        {
            PlanItem ret = new PlanItem();
            ret.AllowUsePC = IsAllowUseComputer ? "1" : "0";
            ret.Context = this.txtContext.Text;
            ret.CreateOn = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            ret.StartTime = this.dtStart.Text;
            ret.EndTime = this.dtEnd.Text;

            return ret;
        }

        /// <summary>
        /// 填充计划项信息
        /// </summary>
        /// <param name="pPlanItem"></param>
        public void FillPlanItem(PlanItem pPlanItem)
        {
            if (pPlanItem == null) return;

            this.dtStart.Text = pPlanItem.StartTime;
            this.dtEnd.Text = pPlanItem.EndTime;
            this.chkUse.Checked = pPlanItem.AllowUsePC == "1";
            this.txtContext.Text = pPlanItem.Context;
        }

        /// <summary>
        /// 校验当前WorkPlanItem输入是否正确
        /// </summary>
        /// <returns></returns>
        public bool IsValidate()
        {
            bool ret;
            if (dtEnd.Value.Ticks > dtStart.Value.Ticks)
            {
                this.errorProvider1.SetError(dtStart, "");
                ret = true;
            }
            else
            {
                string errorMsg= "起始时间要小于结束时间";
                this.SetError(this.dtStart, errorMsg);
                ret = false;
            }

            return ret;
        }

        /// <summary>
        /// 填充WorkPlanItem
        /// </summary>
        /// <param name="pPlanItem"></param>
        public void SetWorkPlanItem(PlanItem pPlanItem)
        {
            this.dtStart.Text = pPlanItem.StartTime;
            this.dtEnd.Text = pPlanItem.EndTime;
            this.txtContext.Text = pPlanItem.Context;
            this.chkUse.Checked = pPlanItem.AllowUsePC == "1";
        }

        /// <summary>
        /// 设置错误
        /// </summary>
        /// <param name="pType"></param>
        /// <param name="pErrorMsg"></param>
        public void SetError(PlanItemInputType pType, string pErrorMsg)
        {
            switch (pType)
            { 
                case PlanItemInputType.Context:
                    break;
                case PlanItemInputType.StartTime:
                    this.SetError(dtStart, pErrorMsg);
                    break;
                case PlanItemInputType.EndTime:
                    this.SetError(dtEnd, pErrorMsg);
                    break;
            }
        }

        private void SetError(Control pControl, string pErrorMsg)
        {
            this.errorProvider1.SetError(pControl, pErrorMsg);
            MessageBox.Show(pErrorMsg);
            pControl.Focus();
        }

        /// <summary>
        /// 清空错误
        /// </summary>
        /// <param name="pType"></param>
        public void ClearError(PlanItemInputType pType)
        {
            switch (pType)
            {
                case PlanItemInputType.Context:
                    break;
                case PlanItemInputType.StartTime:
                    this.errorProvider1.SetError(this.dtStart, "");
                    break;
                case PlanItemInputType.EndTime:
                    this.errorProvider1.SetError(this.dtEnd, "");
                    break;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkDelete_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.parent.DeleteWorkPlanItem(this);
        }
    }

    public enum PlanItemInputType
    { 
        StartTime,
        EndTime,
        Context
    }
}
